﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using CrystalDecisions.CrystalReports.Engine;
namespace Gadgets
{
    public partial class ReportScreen : DockContent
    {
        public ReportScreen()
        {
            InitializeComponent();
        }
        private string report_name = string.Empty;
        public string ReportName
        {
            set
            {
                report_name = value;
            }
        }
        private void report_preview()
        {          
            DataSet report_source = ABSCommonLibrary.DoInquiry(CommonLibrary.CommonLibrary.BOSC011002,
                new object[9] { this.Tag, txtFromDate.Value, txtToDate.Value, txtTransDate.Value,
                    txtAccountNo.Value, cboBoard.Text, cboSecurity.Text, 
                    cboAccountType.Text, txtBankAccount.Text});
            CrystalDecisions.CrystalReports.Engine.ReportClass v_report =
                (CrystalDecisions.CrystalReports.Engine.ReportClass)Activator.CreateInstance(
                Type.GetType(this.report_name, true, true));


            if (report_source.Tables[1].Rows.Count > 0)
            {
                if (Convert.ToInt16(report_source.Tables[1].Rows[0]["error_code"]) != 0)
                {
                    switch (Convert.ToInt16(report_source.Tables[1].Rows[0]["error_code"]))
                    {
                        case -2:
                            ABSCommonLibrary.ShowMessage("So tai khoan khong duoc phep trong");
                            break;
                        default:
                            ABSCommonLibrary.ShowMessage("Error desciption isn't found");
                            break;
                    }
                    return;
                }                
            }
            else
            {
                ABSCommonLibrary.ShowMessage("Error desciption isn't found");
                return;
            }
            v_report.SetDataSource(report_source.Tables[2]);
            foreach (DataRow row in report_source.Tables[0].Rows)
            {
                v_report.SetParameterValue(row["report_parameter"].ToString(),
                    report_source.Tables[1].Rows[0][row["data_field"].ToString()]);
            }            
            v_report.SetParameterValue("@company", DeclareLibrary.COMPANY);
            v_report.SetParameterValue("@address", DeclareLibrary.ADDRESS);
            v_report.SetParameterValue("@website", DeclareLibrary.WEBSITE);
            ReportPreview obj = new ReportPreview();
            obj.Text = this.Text;
            obj.crystalReportViewer1.ReportSource = v_report;
            obj.Show(this.DockPanel, DockState.Document);
            
        }
        private void ReportScreen_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F9)
                {
                    report_preview();
                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtBankAccount.ResetText();
            txtAccountNo.ResetText();
            cboSecurity.ResetText();
            txtFromDate.Value = DateTime.Today;
            txtToDate.Value = DateTime.Today;
            txtTransDate.Value = DateTime.Today;

        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            try
            {
                report_preview();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        public void ResetConfig()
        {
            try
            {
                DataSet controls = ABSCommonLibrary.DoInquiry(CommonLibrary.CommonLibrary.BOSC011001,
                    new object[1] { this.Tag });
                if (controls.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in controls.Tables[0].Rows)
                    {
                        switch (Convert.ToInt16(row["filter_control"]))
                        {
                            case 1:
                                txtFromDate.Enabled = true;
                                break;
                            case 2:
                                txtToDate.Enabled = true;
                                break;
                            case 3:
                                txtTransDate.Enabled = true;
                                break;
                            case 4:
                                txtAccountNo.Enabled = true;
                                break;
                            case 5:
                                cboBoard.Enabled = true;
                                break;
                            case 6:
                                cboSecurity.Enabled = true;
                                break;
                            case 7:
                                cboAccountType.Enabled = true;
                                break;
                            case 8:
                                txtBankAccount.Enabled = true;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
        private void ReportScreen_Load(object sender, EventArgs e)
        {
            ResetConfig();
        }
    }
}